package Question4_1; import CareerCupLibrary.AssortedMethods; import CareerCupLibrary.TreeNode; public class Question { /* The real code for this problem... */ public static int maxDepth(TreeNode root){ if (root == null) { return 0; } return 1 + Math.max(maxDepth(root.left), maxDepth(root.right)); } public static int minDepth(TreeNode root){ if (root == null) { return 0; } return 1 + Math.min(minDepth(root.left), minDepth(root.right)); } public static boolean isBalanced(TreeNode root){ return (maxDepth(root) - minDepth(root) <= 1); } public static void main(String[] args) { // Create balanced tree int[] array = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; TreeNode root = TreeNode.createMinimalBST(array); System.out.println("Root? " + root.data); System.out.println("Is balanced? " + isBalanced(root)); // Could be balanced, actually, but it's very unlikely... TreeNode unbalanced = new TreeNode(10); for (int i = 0; i < 10; i++) { unbalanced.insertInOrder(AssortedMethods.randomIntInRange(0, 100)); } System.out.println("Root? " + unbalanced.data); System.out.println("Is balanced? " + isBalanced(unbalanced)); } }